package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.People;
import com.example.demo.mapper.PeopleMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/people")
@CrossOrigin
public class PeopleController {

    @Resource
    PeopleMapper peopleMapper;


    @PostMapping
    public Result<?> save(@RequestBody People people){
        peopleMapper.insert(people);
        return Result.success();
    }
    @PutMapping
    public Result<?> update(@RequestBody People people){
        peopleMapper.updateById(people);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {

        peopleMapper.deleteById(id);
        return Result.success();
    }
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search,
                              @RequestParam(required = false) Integer id){
        LambdaQueryWrapper<People> wrapper = Wrappers.<People>lambdaQuery();
        if (StrUtil.isNotBlank(search)){
            wrapper.like(People::getName,search);
        }
        if (id != null) { // 判断学号参数是否为空
            wrapper.eq(People::getId,id); // 根据学号查询
        }

        Page<People> peoplePage=peopleMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(peoplePage);
    }


}

